<template>
  <!-- 礼物表 -->
  <el-dialog :visible.sync="isShow" v-if="isShow" title="礼物信息" width="50%" :before-close="handleClose" top="100px" :close-on-click-modal="false">
    <div class="border-box">
      <el-form ref="giftForm" :model="formParams" :rules="giftForm" label-position="right" label-width="0">
        <div class="flex item-box" v-for="(item,index) in itemData" :key="index">
          <div class="title width25">
            {{ item.label }}<span :class="item.pass?'mustPass':'noMustPass'">*</span>
          </div>
          <div class="content width80">
            <el-form-item class="item-clear" v-show="item.key=='game_id'" label="" prop="game_id">
              <el-select v-model="formParams.game_id" placeholder="请选择游戏类型" filterable disabled>
                <el-option label="公共数据" :value="0"></el-option>
                <el-option :label="second.label" :value="second.value" v-for="(second, two) in GAME_TYPE" :key="two"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_base_gift_id'" label="" prop="a_base_gift_id">
              <el-input-number :min="0" :precision="0" v-model="formParams.a_base_gift_id" controls-position="right" clearable placeholder="请输入礼物ID" />
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_gift_type'" label="" prop="a_gift_type">
              <el-select v-model="formParams.a_gift_type" placeholder="请选择礼物类型" filterable >
                <el-option :label="second.label" :value="second.value" v-for="(second, two) in typeData" :key="two"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_gift_remark'" label="" prop="a_gift_remark">
              <el-input type="text" v-model="formParams.a_gift_remark" clearable placeholder="请输入礼物备注" />
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_gift_icon_name'" label="" prop="a_gift_icon_name">
              <el-input class="width40" type="text" v-model="formParams.a_gift_icon_name" clearable placeholder="请输入调用图标资源" />
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_cost_type'" label="" prop="a_cost_type">
              <el-select v-model="formParams.a_cost_type" placeholder="请选择货币类型" filterable >
                <el-option :label="second.label" :value="second.value" v-for="(second, two) in propData" :key="two"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='a_cost_num'" label="" prop="a_cost_num">
              <el-input-number :min="0" :precision="0" v-model="formParams.a_cost_num" controls-position="right" clearable placeholder="请输入购买货币数量"/>
            </el-form-item>
            <el-form-item class="item-clear" v-show="item.key=='is_move'" label="" prop="is_move">
              <el-radio-group v-model="formParams.is_move">
                <el-radio-button :label="1">移动</el-radio-button>
                <el-radio-button :label="0">不移动</el-radio-button>
              </el-radio-group>
            </el-form-item>
          </div>
        </div>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button size="medium" @click="handleClose">取 消</el-button>
      <el-button size="medium" type="primary" @click="handleConfirm('giftForm')">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {
  addGift,
  editGift
} from '@/api/game'
import {
  GAME_TYPE,
  GAME_CURRENCY_TYPE
} from '@/utils/enum'
export default {
  components: {},
  props: {
    show: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default: function () {
        return {}
      }
    },
    propData: {
      type: Array,
      default: function () {
        return []
      }
    },
    gameType: {
      type: Number,
      default: 1
    },
  },
  data () {
    return {
      GAME_TYPE,
      GAME_CURRENCY_TYPE,
      isShow: false,
      itemData:[
        { label: '游戏类型', key: 'game_id' },
        { label: '礼物ID', key: 'a_base_gift_id',pass:true },
        { label: '礼物类型', key: 'a_gift_type' },
        { label: '礼物备注', key: 'a_gift_remark',pass:true },
        { label: '调用图标资源', key: 'a_gift_icon_name' },
        { label: '货币类型', key: 'a_cost_type',pass:true },
        { label: '货币数量', key: 'a_cost_num',pass:true },
        { label: '是否移动', key: 'is_move' },
      ],
      formParams: {
        game_id:this.gameType,
        a_base_gift_id: '', //礼物id
        a_gift_remark: '', //备注
        a_gift_type: 1, //礼物类型
        a_gift_icon_name: '', //动画调用路径
        a_cost_type: '', //购买货币类型
        a_cost_num: 0, //购买货币数量
        is_move: 0, //是否移动
      },
      giftForm: {
        a_base_gift_id: [{ required: true, trigger: 'blur', message: '请输入礼物id', }],
        a_gift_remark: [{ required: true, trigger: 'blur', message: '请输入备注', }],
        a_cost_type: [{ required: true, trigger: 'blur', message: '请选择购买货币类型', }],
        a_cost_num: [{ required: true, trigger: 'blur', message: '请输入购买货币数量', }],
      },
      roleDataList: [],
      typeData: [
        {label:'好礼物',value:1},
        {label:'坏礼物',value:2},
      ],
    }
  },
  computed: {},
  watch: {
    show: function () {
      let that = this;
      that.isShow = that.show
      if (that.data) {
        that.formParams = JSON.parse(JSON.stringify(that.data))
      } else {
        that.formParams = {
          game_id:this.gameType,
          a_base_gift_id: '', 
          a_gift_remark: '',
          a_gift_icon_name: '',
          a_gift_type: 1, //礼物类型
          a_cost_type: '',
          a_cost_num: 0,
          is_move: 0,
        }
      }
    },
  },
  mounted () {},
  methods: {
    // 确认
    handleConfirm (formvalue) {
      let that = this, params = JSON.parse(JSON.stringify(that.formParams));
      params.a_cost_num=that.$public.mathNumberMultiply(params.a_cost_num,10000)
      that.$refs[formvalue].validate((valid) => {
        if (valid) {
          // 新增
          if (!params.id) {
            addGift(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          } else {
            editGift(params).then(function (res) {
              that.$message.success(res.msg)
              that.$emit('onHideDetails', true)
            }).catch((error) => {
              that.$message.error(error.msg)
            })
          }
        } else {
          return false;
        }
      });
    },
    // 取消
    handleClose () {
      this.$emit('onHideDetails', false)
    },
  }
}
</script>

<style scoped lang="scss">
.encryption {
  height: 10px;
  line-height: 10px;
  margin-bottom: -10px;
  font-size: 12px;
  color: darkred;
}
</style>
